package com.yc.spring;

import com.yc.Demo;
import com.yc.spring.biz.UserBiz;
import com.yc.spring.mapper.UserMapper;
import com.yc.spring.web.UserAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;

//注解-》给jvm读取的信息
@ComponentScan("com.yc.spring")
@Configuration
public class AnnoConfig {
    // @Bean->   <bean id="demo" class="com.yc.Demo"></bean>
    @Bean(initMethod = "init",destroyMethod = "destroy")
    @Lazy //懒加载，到用到这个类才加载
    //默认的是单例模式->每次用的都是一个对象，这个注解-》原型模式
    @Scope("prototype")
    public Demo demo(){
        Demo demo = new Demo();
        demo.setName("张三");
        return demo;
    }
    //在根据demo.class创建时，这里有多个bean,不知道创哪个，加上这个-》默认bean
    @Primary
    @Bean(initMethod = "init",destroyMethod = "destroy")
    //创键默认顺序是从上往下的
    @DependsOn("newDemo1") //让newDemo1先创建
    public Demo newDemo(){
        Demo demo = new Demo();
        demo.setName("李四");
        return demo;
    }
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Demo newDemo1(){
        Demo demo = new Demo();
        demo.setName("王武");
        return demo;
    }
    @Bean
    public UserMapper userMapper(){
        return new UserMapper();
    }
    //@Autowired表示从IOC容器中获取bean注入到该属性中
    @Autowired
    UserMapper userMapper;
    @Bean
    public UserBiz userBiz(){
        UserBiz userBiz = new UserBiz();
        userBiz.setUserMapper(userMapper);
        return userBiz;
    }
    //Bean 方法参数对象 Spring会从IOC容器中获取一个对应bean注入进来
    //这个叫接口注入
    @Bean
    public UserAction userAction(UserBiz userBiz){
        UserAction action = new UserAction();
        action.setUserBiz(userBiz);
        return  action;
    }
}
